package net.cn_su.sharpeyes.live;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import net.cn_su.sharpeyes.beans.Camera;
import net.cn_su.sharpeyes.components.CameraItem;
import net.cn_su.sharpeyes.components.CameraItem_;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Overlord on 2016/08/11.
 * SingularityClub
 */
@EBean
public class CameraAdapter extends BaseAdapter {

    private List<Camera> cameras = new ArrayList<>();
    private int selectedPosition = -1;// 选中的位置

    @RootContext
    protected Context context;

    public void setCameras(List<Camera> cameras) {
        this.cameras.clear();
        if(cameras != null) {
            this.cameras.addAll(cameras);
        }
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    /**
     * 获取选中的摄像头对象
     * @return
     */
    public Camera getSelectedCamera() {
        return cameras.size() > 0 ? cameras.get(selectedPosition) : null;
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    @Override
    public int getCount() {
        return cameras.size();
    }

    @Override
    public Camera getItem(int i) {
        return cameras.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        CameraItem item = null;
        if(view != null){
            item = (CameraItem) view;
        }else{
            item = CameraItem_.build(context);
        }
        Camera camera = getItem(i);
        camera.setPlaying(selectedPosition == i);
        item.setCamera(camera);
        return item;
    }
}
